<?php
/**
 * Created by 项目.
 * User: xuguopeng
 * Date: 2020-06-14
 * Time: 22:49
 * 项目孵化
 */

namespace app\api\controller\v1;


use app\api\controller\BaseController;
use app\api\model\ProjectHatch;
use app\api\service\project\HatchOrderSer;
use app\api\service\project\HatchSer;
use app\api\validate\Paging;
use app\api\service\Token;
use think\facade\Request;

class Project extends BaseController
{
    protected $beforeActionList = [
        'checkPrimaryScope' => ['only' => 'getProjectList']
    ];

    // 获取项目孵化列表
    public function getProjectList($page,$limit)
    {
        (new Paging())->goCheck();
        $query = ProjectHatch::withCount('comments')->where('status','=',2)
            ->where('agree_status','=',2)
            ->order('id desc');
        $count = $query->count();
        $result = $query->page($page, $limit)->hidden(['phone', 'update_time','delete_time'])->select()->toArray();
        $data['count'] = $count;
        $data['data'] = $result;
        return $data;
    }

    // 判断是否有发布项目的机会
    public function judgeProject()
    {
        $info = Token::judgeProjectHatch();
        if($info) {
            return [
                'isValid' => true
            ];
        } else {
            return [
                'isValid' => false
            ];
        }
    }

    // 创建订单
    public function createOrder()
    {
        $hatchOrder = new HatchOrderSer();
        return $hatchOrder->createOrder();
    }

    // 创建项目孵化信息修改项目孵化订单
    public function createData()
    {
        $post = Request::post();
        $hatchSer = new HatchSer($post);
        return $hatchSer->entrance();
    }

    // 获取用户项目孵化列表
    public function getProjectListByUser()
    {
         $uid = Token::getCurrentUid();
         return ProjectHatch::where('user_id',$uid)->order('id desc')
             ->visible(['id', 'user_id','title','username','budget','create_time'])
             ->select()->toArray();
    }

    // 获取用户项目孵化详情
    public function getProjectInfoByUser($id)
    {
        $uid = Token::getCurrentUid();
        return ProjectHatch::where('user_id',$uid)
            ->with('comments')
            ->where('id',$id)
            ->find();
    }
}